﻿using System;
using System.Windows.Forms;

using Moe.ECamel.Common.Entities;
using Moe.ECamel.Client.Controllers;
using Moe.ECamel.Client.WinForms.Common;

namespace Moe.ECamel.Client.WinForms.Controls
{
	public partial class SubscriptionDetailsControl : ECamelUserControl
	{
		public SubscriptionDetailsControl()
		{
			InitializeComponent();
		}

		private DetailsMode _detailsMode;
		public DetailsMode DetailsMode
		{
			get { return _detailsMode; }
			set
			{
				this._detailsMode = value;
				ActiveCheckBox.Enabled = (value != DetailsMode.View);
				SubscriptionItemsDataGridView.ReadOnly = (value != DetailsMode.Create);
			}
		}

		public void Bind(Subscription entity)
		{
			SubscriptionBindingSource.DataSource = entity;
			SubscriptionItemsBindingSource.DataSource = entity.SubscriptionItemTypeInfos;
			SubscriptionIdTextBox.Bind(SubscriptionBindingSource, "SubscriptionId");
			ActiveCheckBox.Bind(SubscriptionBindingSource, "Active");
			ActivationDateTimePicker.Bind(SubscriptionBindingSource, "ActivationDate");
			ExpirationDateTimePicker.Bind(SubscriptionBindingSource, "ExpirationDate");
			TotalPriceTextBox.Bind(SubscriptionBindingSource, "TotalPrice");
			TotalDepositTextBox.Bind(SubscriptionBindingSource, "TotalDeposit");
		}
		public void CancelEdit()
		{
			this.SubscriptionBindingSource.CancelEdit();
			this.SubscriptionItemsBindingSource.CancelEdit();
		}

		private void SubscriptionItemsDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			SubscriptionItemsBindingSource.CurrencyManager.Refresh();
			SubscriptionBindingSource.CurrencyManager.Refresh();
		}
	}
}